class Solution {
    public String convert1(String s, int numRows) {
        int len = s.length();
        StringBuilder stringBuilder = new StringBuilder();
        char[][] matrix = new char[numRows][len];
        int row = 0;
        int col = 0;
        for(int i = 0; i < len; i++){
            char ch = s.charAt(i);
            int j = i % (2 * (numRows - 1));
            if(j < numRows){
                matrix[row][col] = ch;
                row++;
                if(row == numRows){
                    row -= 2;
                    col++;
                }
            }else{
                matrix[row][col] = ch;
                row--;
                col++;
            }
        }
        for(int i = 0; i < numRows; i++){
            for(int j = 0; j < len; j++){
                char ch = matrix[i][j];
                if(ch != 0){
                    stringBuilder.append(ch);
                }
            }
        }
        return stringBuilder.toString();
    }

    public String convert(String s, int numRows) {
        if(numRows == 1) return s;
        StringBuilder stringBuilder = new StringBuilder();
        //找第一行的字符
        int d = 2 * numRows - 2;
        for(int i = 0; i < numRows; i++){
            if((i == 0) || (i == numRows - 1)){
                int index = i;
                while(index < s.length()){
                    char ch = s.charAt(index);
                    stringBuilder.append(ch);
                    index += d;
                }
            }else{
                int index1 = i;
                int index2 = d - i;
                while(index1 < s.length() || index2 < s.length()){
                    if(index1 < s.length()){
                        char ch1 = s.charAt(index1);
                        stringBuilder.append(ch1);
                        index1 += d;
                    }
                    if(index2 < s.length()){
                        char ch2 = s.charAt(index2);
                        stringBuilder.append(ch2);
                        index2 += d;
                    }
                }
            }
        }
        return stringBuilder.toString();
    }
}